package com.mobile.service.impl.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.mobile.service.dto.FunctionDTO;
import com.mobile.service.dto.FunctionQueryDTO;
import com.mobile.service.dto.RoleDTO;
import com.mobile.util.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mobile.service.dao.DaoSupport;
import com.mobile.service.inter.system.FunctionService;

/**
 * 功能菜单管理Service实现
 * @author Administrator
 *
 * @version 0.1 2021-01-15 初版
 *
 */
@Service("functionService")
@Transactional
public class FunctionServiceImpl implements FunctionService {
	@Resource(name = "daoSupport")
	private DaoSupport dao;

	/**获取角色对应单功能权限数据
	 * @param pd  参数
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<PageData> getRoleFunctionAuthData(PageData pd) throws Exception {
		return (List<PageData>)dao.findForList("MFunctionMapper.getRoleFunctionAuthData", pd);

	}

	/**获取用户账号获取全功能的权限对应关系数据
	 * @param pd  参数
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<PageData> getFunctionAuthData(PageData pd) throws Exception {
		return (List<PageData>)dao.findForList("MFunctionMapper.getFunctionAuthData", pd);

	}

	/**根据用户账号获取全部功能权限数据
	 * @param pd  参数
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getAuthDataByUserCode(PageData pd) throws Exception {
		return (List<String>)dao.findForList("MFunctionMapper.getAuthDataByUserCode", pd);

	}

	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(系统菜单列表)(递归处理)(有效值筛选)
	 * @param functionId
	 * @return
	 * @throws Exception
	 */
	public List<PageData> listAllMenuQx(String functionId) throws Exception {
		List<PageData> menuList = this.listSubMenuByParentId(functionId);
		for(PageData menu : menuList){
			menu.put("subMenu", this.listAllMenuQx(menu.get("function_id").toString()));
			menu.put("target", "treeFrame");
		}
		return menuList;
	}
	/**
	 * 获取所有菜单并填充每个菜单的子菜单列表(系统菜单列表)(递归处理)
	 * @param functionId
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> listAllMenu(String functionId) throws Exception {
		List<PageData> menuList = this.listSubMenuByParentId2(functionId);
		for(PageData menu : menuList){
			menu.put("subMenu", this.listAllMenu(menu.get("function_id").toString()));
			menu.put("target", "treeFrame");
		}
		return menuList;
	}
	/**
	 * 功能菜单页面左边树展示
	 * @param functionId
	 * @return
	 * @throws Exception
	 */
	public List<PageData> leftMenuTreeList(String functionId) throws Exception {
		List<PageData> menuList = this.listSubMenuByParentId2(functionId);
		for(PageData menu : menuList){
			menu.put("subMenu", this.listAllMenu(menu.get("function_id").toString()));
			menu.put("target", "treeFrame");
		}
		return menuList;
	}
	/**
	 * 通过ID获取其子一级菜单(有效值筛选)
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listSubMenuByParentId(String parentId) throws Exception {
		return (List<PageData>) dao.findForList("MFunctionMapper.listSubMenuByParentId", parentId);
	}
	/**
	 * 通过ID获取其子一级菜单
	 * @param parentId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listSubMenuByParentId2(String parentId) throws Exception {
		return (List<PageData>) dao.findForList("MFunctionMapper.listSubMenuByParentId2", parentId);
	}
	/**
	 * 菜单列表(分页)
	 */
	@Override
	public PageResult<FunctionDTO> getMenuList(FunctionQueryDTO functionQueryDTO)throws Exception {
		return new PageResult<FunctionDTO>((List<FunctionDTO>) dao.findForList("MFunctionMapper.mfunctionlist",functionQueryDTO));
	}
	/**
	 * 新增功能菜单
	 * @param pageData
	 * @return
	 */
	@Override
	public Map<String, Object> insertFunction(PageData pageData) throws Exception {
		Map<String, Object> map = new HashMap<>();
		//验证菜单名称是否重复
		Object pds = dao.findForObject("MFunctionMapper.mfunctionList", pageData);
		if (pds!=null) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该功能名称"));
			return map;
		}
		Object rbp = dao.findForObject("MFunctionMapper.findRepeatByPermis", pageData);
		if (rbp!=null) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该功能代号"));
			return map;
		}
		//判断排序是否为空,为空则默认0
		if (StringUtil.isEmpty(pageData.get("sortNo"))) {
			pageData.put("sortNo", 0);
		}
		dao.save("MFunctionMapper.insertMFunction",pageData);
		//将新增功能菜单初始化值插入用户角色权限关联表与角色权限关联表中
		String functionId = pageData.getString("functionId");
		String userCode = pageData.get("createUser").toString();
		List<PageData> userList=(List<PageData>) dao.findForList("UserMapper.userlist", null);
		List<RoleDTO> roleList=(List<RoleDTO>) dao.findForList("RoleMapper.getRoleList", null);
		List<PageData> authList=(List<PageData>) dao.findForList("AuthMapper.authListAll", null);
		List<PageData> userAuthList = new ArrayList<>();
		List<PageData> roleAuthList = new ArrayList<>();
		PageData userAuth;
		PageData roleAuth;
		for (PageData data : authList) {
			//用户角色权限列表
			for (PageData user : userList) {
				userAuth = new PageData();
				userAuth.put("functionId", functionId);
				userAuth.put("isAuth", "0");
				userAuth.put("createUser", userCode);
				userAuth.put("updateUser", userCode);
				userAuth.put("authId", data.getString("key"));
				userAuth.put("roleId", user.get("roleId").toString());
				userAuth.put("userCode", user.get("userCode").toString());
				userAuthList.add(userAuth);
			}
			//角色权限列表
			for (RoleDTO role : roleList) {
				roleAuth = new PageData();
				roleAuth.put("functionId", functionId);
				roleAuth.put("isAuth", "0");
				roleAuth.put("createUser", userCode);
				roleAuth.put("updateUser", userCode);
				roleAuth.put("authId", data.getString("key"));
				roleAuth.put("roleId", role.getRoleId().toString());
				roleAuthList.add(roleAuth);
			}
		}
		//插入新增菜单的用户权限数据
		dao.batchSave("UserMapper.addUserAuth", userAuthList);
		//插入新增菜单的角色权限数据
		dao.save("RoleMapper.insertRoleAuth", roleAuthList);
		map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
		map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
		return map;
	}
	/**
	 * 修改功能菜单
	 * @param pageData
	 * @return
	 */
	@Override
	public Map<String, Object> updateFunction(PageData pageData) throws Exception {
		Map<String, Object> map = new HashMap<>();
		//判断值是否存在
		Object obj = dao.findForObject("MFunctionMapper.getFunctionById", pageData.get("functionId").toString());
		if (StringUtil.isEmpty(obj)) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
			return map;
		}
		//验证菜单名称是否重复
		Object pds = dao.findForObject("MFunctionMapper.mfunctionList", pageData);
		if (!StringUtil.isEmpty(pds)) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该菜单名称"));
			return map;
		}
		Object rbp = dao.findForObject("MFunctionMapper.findRepeatByPermis", pageData);
		if (rbp!=null) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该功能代号"));
			return map;
		}
		dao.update("MFunctionMapper.updateMFunction",pageData);
		map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
		map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
		return map;
//		return (int) dao.save("MFunctionMapper.insertMFunction",pageData);
	}
	/**
	 * 修改功能菜单是否有效
	 * @param pageData
	 * @return
	 */
	@Override
	public Map<String, Object> editEffective(PageData pageData) throws Exception {
		Map<String, Object> map = new HashMap<>();
		dao.save("MFunctionMapper.updateEffective",pageData);
		map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
		map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
		return map;
	}

	/**
	 * 通过id查询功能菜单
	 * @param functionId
	 * @return
	 * @throws Exception
	 */
	@Override
	public PageData findFunctionById(String functionId)throws Exception {
		return (PageData) dao.findForObject("MFunctionMapper.getFunctionById", functionId);
	}

	/**
	 * 删除菜单
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> removeFunc(PageData pd) throws Exception {
		Map<String, Object> map = new HashMap<>();
		List<PageData> funcList = (List<PageData>) dao.findForList("MFunctionMapper.listSubMenuByParentId2", pd.getString("functionId"));
		//如果菜单有子菜单,则提示无法删除
		if (!funcList.isEmpty()) {
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_EXCEPTION);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0071"));
		} else {
			//删除菜单以及其关联表
			dao.delete("MFunctionMapper.deleteUserAuthByFuncId", pd.getString("functionId"));
			dao.delete("MFunctionMapper.deleteRoleAuthByFuncId", pd.getString("functionId"));
			dao.delete("MFunctionMapper.deleteFuncById", pd.getString("functionId"));
			map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
			map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
		}
		return map;
	}
}
